package algorithm;

import org.junit.Test;

/**
 * @Author: ycy
 * @CreateDate: 2020/6/29 12:58
 */
public class Size {

  @Test
  public void test() {

    System.out.println(tableSizeFor(11));

    System.out.println(tableSizeFor(100));

    System.out.println(tableSizeFor(1000));
  }

  int MAXIMUM_CAPACITY = 1 << 30;

  int tableSizeFor(int cap) {
    int n = cap - 1;
    n |= n >>> 1; //不带符号右移三位
    n |= n >>> 2;
    n |= n >>> 4;
    n |= n >>> 8;
    n |= n >>> 16;
    return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;
  }

}
